package com.fileupload.upload.log;

import com.fileupload.model.entity.FileUploadLog;
import com.fileupload.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 文件上传日志控制器
 * 提供查询上传日志的接口，支持分页查询和单条详情查询
 * @author 小乙
 */
@RestController
@RequestMapping("/api/upload/log")
public class FileUploadLogController {
    
    private static final Logger logger = LoggerFactory.getLogger(FileUploadLogController.class);
    
    @Autowired
    private FileUploadLogService fileUploadLogService;

    /**
     * 获取文件上传日志列表
     * @param page 页码
     * @param size 每页数量
     * @return 日志列表和分页信息
     */
    @GetMapping("/list")
    public Result<List<FileUploadLog>> getLogList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        logger.info("[日志查询] 收到获取日志列表请求 - 页码: {}, 每页数量: {}", page, size);
        
        try {
            long startTime = System.currentTimeMillis();
            Result<List<FileUploadLog>> result = fileUploadLogService.getLogList(page, size);
            long duration = System.currentTimeMillis() - startTime;
            
            logger.info("[日志查询] 获取日志列表成功 - 返回数据条数: {}, 耗时: {}ms, 总条数: {}", 
                    result.getData() != null ? result.getData().size() : 0, 
                    duration, 
                    result.getTotal());
            return result;
        } catch (Exception e) {
            logger.error("[日志查询] 获取日志列表失败 - 页码: {}, 每页数量: {}, 错误信息: {}", 
                    page, size, e.getMessage(), e);
            throw e;
        }
    }
    
    /**
     * 根据ID获取单个日志详情
     * @param id 日志ID
     * @return 日志详情
     */
    @GetMapping("/detail")
    public Result<FileUploadLog> getLogDetail(@RequestParam Long id) {
        logger.info("[日志查询] 收到获取日志详情请求 - 日志ID: {}", id);
        
        try {
            long startTime = System.currentTimeMillis();
            Result<FileUploadLog> result = fileUploadLogService.getLogDetail(id);
            long duration = System.currentTimeMillis() - startTime;
            
            logger.info("[日志查询] 获取日志详情{} - 日志ID: {}, 耗时: {}ms", 
                    result.getCode() == 200 ? "成功" : "失败", 
                    id, 
                    duration);
            return result;
        } catch (Exception e) {
            logger.error("[日志查询] 获取日志详情失败 - 日志ID: {}, 错误信息: {}", id, e.getMessage(), e);
            throw e;
        }
    }
}